-
Notifications
You must be signed in to change notification settings - Fork 8.2k
[zh-CN] add JavaScript.Reference.Errors.BigInt_division_by_zero #27344
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
69d0602
[zh-CN] add js-errors-BigInt_division_by_zero
TouyamaRie e925690
Merge branch 'mdn:main' into main
TouyamaRie 8300ec4
Update files/zh-cn/web/javascript/reference/errors/bigint_division_by…
TouyamaRie d02ef3f
Update files/zh-cn/web/javascript/reference/errors/bigint_division_by…
TouyamaRie 6cd1d22
Update index.md
jasonren0403 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
files/zh-cn/web/javascript/reference/errors/bigint_division_by_zero/index.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
--- | ||
title: "RangeError: BigInt division by zero" | ||
slug: Web/JavaScript/Reference/Errors/BigInt_division_by_zero | ||
--- | ||
|
||
{{jsSidebar("Errors")}} | ||
|
||
当一个 {{jsxref("BigInt")}} 被 `0n` 除时,会产生 JavaScript 异常“BigInt division by zero”。 | ||
|
||
## 错误信息 | ||
|
||
```plain | ||
RangeError: Division by zero (V8-based) | ||
RangeError: BigInt division by zero (Firefox) | ||
RangeError: 0 is an invalid divisor value (Safari) | ||
``` | ||
|
||
## 错误类型 | ||
|
||
{{jsxref("RangeError")}} | ||
|
||
## 哪里出错了? | ||
|
||
当使用[除法](/zh-CN/docs/Web/JavaScript/Reference/Operators/Division)或者[取余](/zh-CN/docs/Web/JavaScript/Reference/Operators/Remainder)运算符时,如果除数为 `0n` 则会触发该错误。在 {{jsxref("Number")}} 运算中,除以 `0n` 会得到 [`Infinity`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Infinity),但在 BigInt 中不存在“Infinity”这一值,因此会抛出错误。在使用除法前,请先检查除数是否为 `0n`。 | ||
|
||
## 示例 | ||
|
||
### 被 0n 除 | ||
|
||
```js example-bad | ||
const a = 1n; | ||
const b = 0n; | ||
const quotient = a / b; | ||
// RangeError: BigInt division by zero | ||
``` | ||
|
||
相反,应首先检查除数是否为 `0n`,并给出更友好的提示或者使用其他值,例如 `Infinity` 或 `undefined`。 | ||
|
||
```js example-good | ||
const a = 1n; | ||
const b = 0n; | ||
const quotient = b === 0n ? undefined : a / b; | ||
``` | ||
|
||
## 相关内容 | ||
TouyamaRie marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
- [`BigInt`](/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/BigInt) | ||
- [除法(`/`)](/zh-CN/docs/Web/JavaScript/Reference/Operators/Division) | ||
- [取余(`%`)](/zh-CN/docs/Web/JavaScript/Reference/Operators/Remainder) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.